package com.zqk.pattern.creational.singleton.doubelcheck;

/**
 * @Package com.zqk.pattern.creational.singleton
 * @Description:TODO
 * @Author 张乾坤
 * @Create: 2018/10/26
 * @Version 1.0
 * @Update: message
 */
public class LazyDoubleCheckSingleton {
    private volatile static LazyDoubleCheckSingleton lazyDoubleCheckSingleton = null;//使变量在多个线程间可见（可见性） volatile关键字修饰的变量不会被指令重排序优化。
    private LazyDoubleCheckSingleton() {

    }

    public  static LazyDoubleCheckSingleton getInstance() {
        if (lazyDoubleCheckSingleton == null) {//对象不为空，但是还没有完成初始化21行没有执行完
                synchronized (LazyDoubleCheckSingleton.class){
                    if (lazyDoubleCheckSingleton == null) {
                        lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
                        //1.分配内存给这个对象
                        //2.初始化对象
                     //3.设置lazyDoubleCheckSingleton 指向刚分配的内存地址   23可能重排序

                    }
                }

        }
        return lazyDoubleCheckSingleton;
    }
}
